/**
 * 
 */

package org.spring.cloud.pilot.endpoint;

import java.util.Collections;
import java.util.Map;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;

import com.netflix.appinfo.InstanceInfo;

/**
 * 
 * ClassName: UpEndpoint <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2019年10月23日 下午4:03:12 <br/>
 *
 * @author admin
 * @version
 * @since JDK 1.8
 */
@RefreshScope
@ConfigurationProperties("endpoints.up")
@ManagedResource
public class UpEndpoint extends AbstractEndpoint<Map<String, Object>>
    implements ApplicationListener<ApplicationPreparedEvent> {

    private ConfigurableApplicationContext context;

    private SpringApplication application;

    private String[] args;

    private ApplicationPreparedEvent event;

    private ServiceRegistry serviceRegistry;

    private Registration registration;

    public UpEndpoint(String id) {

        super("up", true, false);
        // TODO Auto-generated constructor stub

    }

    @Override
    public Map<String, Object> invoke() {

        if (registration == null) {
            //ResponseEntity.status(HttpStatus.NOT_FOUND).body("no registration found");
            return Collections
                .unmodifiableMap(Collections.<String, Object>singletonMap("message", "registration id null , bye..."));
        }
        serviceRegistry.setStatus(registration, InstanceInfo.InstanceStatus.UP.name());
        if (!isRunning())
            context.start();
        return Collections
            .unmodifiableMap(Collections.<String, Object>singletonMap("message", "uping , bye..."));

    }

    @Override
    public void onApplicationEvent(ApplicationPreparedEvent input) {
        this.event = input;
        if (this.context == null) {
            this.context = this.event.getApplicationContext();
            this.args = this.event.getArgs();
            this.application = this.event.getSpringApplication();
            this.serviceRegistry = this.context.getBean(ServiceRegistry.class);
            this.registration = this.context.getBean(Registration.class);

        }
    }

    @ManagedAttribute
    public boolean isRunning() {
        if (this.context != null) {
            return this.context.isRunning();
        }
        return false;
    }

    @ManagedOperation
    public synchronized void pause() {
        if (this.context != null) {
            this.context.stop();
        }
    }

    @ManagedOperation
    public synchronized void resume() {
        if (this.context != null) {
            this.context.start();
        }
    }

}
